<?php

class DefaultModel {

	private $connection;
	private $result;
	private $return;

	public function __construct()
	{
		$this->connection =& Database::getInstance();
	}

	public function query( $sql )
	{
		$this->result = $this->connection->query( $sql);
	}

	public function getAll( $index = null, $type = null )
	{
		$concat = 'fetchAll' . $type;
		if ( $type && $index )
			$this->$concat( $index );
		elseif ( $type )
			$this->$concat();
		elseif ( $index )
			$this->fetchAllObject( $index );
		else
			$this->fetchAllObject();

		return $this->return;
	}

	public function fetchAllObject( $index = null  )
	{
		if ( $this->result->num_rows == 1 )
			$this->return = $this->result->fetch_object();

		while( $row = $this->result->fetch_object() )
		{
			if ( $index )
				$this->return[ $row->$index ] = $row;
			else
				$this->return[] = $row;
		}
	}

	public function fetchAllAssoc( $index = null  )
	{
		if ( $this->result->num_rows == 1 )
			$this->return = $this->result->fetch_array();

		while( $row = $this->result->fetch_array() )
		{
			if ( $index )
				$this->return[ $row[ $index ] ] = $row;
			else
				$this->return[] = $row;
		}
	}

}

?>